#include "Stdafx.h"
#include "PromptDataSource.h"

#pragma comment(lib, "Ole32.lib")

LPWSTR PromptDataSource(LPWSTR  InitialString) 
{
  CoInitializeEx(NULL, COINIT_MULTITHREADED);

   // First CoCreate the OLE DB Service Component Manager.
   HRESULT hr = S_OK;
   IDataInitialize *  pIDataInitialize = NULL;
   IUnknown * pIDataSource =NULL;
   hr = CoCreateInstance(CLSID_DataLinks, NULL, CLSCTX_INPROC_SERVER, IID_IDataInitialize, reinterpret_cast<void **>(&pIDataInitialize));

   if (InitialString != NULL)
	   pIDataInitialize->GetDataSource(NULL, CLSCTX_INPROC_SERVER,
	   LPCOLESTR(InitialString), IID_IUnknown, reinterpret_cast<IUnknown **>(&pIDataSource));

   IDBPromptInitialize *pIDBPromptInitialize = NULL;
   hr = pIDataInitialize ->QueryInterface(IID_IDBPromptInitialize, reinterpret_cast<void **>(&pIDBPromptInitialize)); 

   hr = pIDBPromptInitialize->PromptDataSource( NULL, NULL, DBPROMPTOPTIONS_PROPERTYSHEET,
      0, NULL, NULL, IID_IDBProperties, reinterpret_cast<IUnknown **>(&pIDataSource));

   if(SUCCEEDED(hr))
   {
	   pIDataInitialize->GetInitializationString(reinterpret_cast<IUnknown *>(pIDataSource), true, (LPOLESTR *)&InitialString);
   }
 
   pIDataSource->Release();
   pIDataInitialize->Release();
   pIDBPromptInitialize->Release();
   CoUninitialize();
   return InitialString;
}